package com.qf.service.impl;

import com.qf.dto.ContractorDto;
import com.qf.mapper.ContractorMapper;
import com.qf.model.Contractor;
import com.qf.model.Project;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.ContractorService;
import com.qf.service.ProjectService;
import com.qf.utils.UuidUtils;
import com.qf.vo.ConProVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.util.List;

@Service
public class ContractorServiceImpl implements ContractorService {
    @Autowired
    private ContractorMapper contractorMapper;
    @Override//分页查询
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        List<Contractor> contractors = contractorMapper.findPage((currentPage - 1) * pageSize, pageSize);
        Long count = contractorMapper.count();
        pageResult.setRows(contractors);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Autowired
    private ProjectService projectService;
    @Override
    //添加开发商的同时还要将工程进行绑定
    @Transactional //fromencode方式
    public int addAndBind(String cname, int[] pids) {
        //添加承建商
        Contractor contractor =new Contractor();
        contractor.setCid(UuidUtils.uuid());
        contractor.setCname(cname);
        contractor.setUpdatetime(new Date());
        int row = contractorMapper.addContractor(contractor);
        //绑定工程
        projectService.bindCid(contractor.getCid(),pids);

        return row;
    }

    @Override
    @Transactional
    public int addContractor(ConProVO conProVO) {
        Contractor contractor =new Contractor();
        contractor.setCid(UuidUtils.uuid());
        contractor.setCname(conProVO.getCname());
        contractor.setUpdatetime(new Date());
        int row = contractorMapper.addContractor(contractor);
        projectService.bindCid(contractor.getCid(),conProVO.getPids());
        return row;
    }

    @Override
    public List<Contractor> getAll() {
        List<Contractor> list=contractorMapper.findAll();
        return list;
    }

    @Override
    public Contractor findByCid(String cid) {
        //cname cid
        Contractor contractor = contractorMapper.findByCid(cid);
        //pids
        List<Project> list = projectService.findByCid(cid);

        return contractor;

    }

    @Override
    @Transactional
    public int edit(ContractorDto contractorDto) {
        int row = contractorMapper.edit(contractorDto);
        //同时更新project的cid
        projectService.deleteCid(contractorDto.getCid());
        //绑定工程
        projectService.bindCid(contractorDto.getCid(),contractorDto.getPids());
        return row;
    }

    @Override
    @Transactional
    public int deleteByCid(String cid) {
        int row = contractorMapper.delByCid(cid);
        //同时更新project的cid
        projectService.deleteCid(cid);
        return row;
    }




}
